<?php
class Zend_Controller_Action_Helper_Item extends Zend_Controller_Action_Helper_Abstract
{

    public function viewNum($view,$itemid)
    {

        if(is_numeric($itemid))
        {

            $item=new CItem();

            if($_COOKIE[COOKIE_PRE.'product'.$itemid]!=1)
            {

                $item->addViewNum($itemid);

                setcookie(COOKIE_PRE.'product'.$itemid,'1',time()+3600,'/');

            }

            $view->assign('itemViewNum',$item->getViewNum($itemid));

        }else
        {

            $view->assign('itemViewNum',0);

        }
    }
    public function getLagnappeArray()
    {
        $view=$this->getActionController()->view;
        $cookieString=$_COOKIE[COOKIE_PRE.'product_buy_list'];

        if($cookieString!='')
        {
            $shoppingArray=json_decode(stripslashes($cookieString),true);

        }

        $item=new CItem();
        $CLagnappe=new CLagnappe();

        $LagnappeArray=array();
        $fuidkeyArray=array();
        $totalPrice=0;

        foreach($shoppingArray as $key => $value)
        {
            $totalcounts = $shoppingArray[$key]['counts'];
            // echo($key.'--');
            // echo($item->getitemfuid($item->getitemid($key)).'--');
            foreach($shoppingArray as $keya => $valuea)
            {
                if($key && $keya)
                {
                    if($key != $keya)
                    {
                        if ($item->getitemfuid($item->getitemid($key)) && $item->getitemfuid($item->getitemid($keya)) && $item->getitemfuid($item->getitemid($key)) == $item->getitemfuid($item->getitemid($keya)))
                        {
                            $totalcounts=$totalcounts+$shoppingArray[$keya]['counts'];
                        }
                    }
                }
            }
            $fuidkey=$item->getitemfuid($item->getitemid($key));
            if($fuidkeyArray)
            {
                $k   =   array_search($fuidkey,$fuidkeyArray);
                if($k   ===   false)
                {
                    $LagnappeArraytmp=$CLagnappe->getItemsByItemid($fuidkey,$totalcounts);
                    if($LagnappeArraytmp)
                    {
                        for($i=0;$i<count($LagnappeArraytmp);$i++)
                        {
                            array_unshift($LagnappeArray,$LagnappeArraytmp[$i]);
                        }
                    }
                }
            }
            else
            {
                $LagnappeArraytmp=$CLagnappe->getItemsByItemid($fuidkey,$totalcounts);
                if($LagnappeArraytmp)
                {
                    for($i=0;$i<count($LagnappeArraytmp);$i++)
                    {
                        array_unshift($LagnappeArray,$LagnappeArraytmp[$i]);
                    }
                }
            }

            array_unshift($fuidkeyArray,$fuidkey);


        }

       $view->assign('LagnappeArray',$LagnappeArray);


            $attachment=new CLagnappeAttachment();

            $attachmentArray = array();
            


            for($i=0;$i<count($LagnappeArray);$i++)
            {
             $attachmentArray[$LagnappeArray[$i]["pid"]]= $attachment->getAttachments($LagnappeArray[$i]["pid"]);
             }
            
         $view->assign('attachmentArray',$attachmentArray);


        return array('LagnappeArray'=>$LagnappeArray,'attachmentArray'=>$attachmentArray);


    }

    public function getOrderItemArray()
    {

        $view=$this->getActionController()->view;

        ini_set('memory_limit','-1');

        $itemArray=array();

        $totalPrice=0;

        $cookieString=$_COOKIE[COOKIE_PRE.'product_buy_list'];
        //echo $cookieString;
        if($cookieString!='')
        {
            $shoppingArray=json_decode(stripslashes($cookieString),true);

            $item=new CItem();

            $itemidAmountsArray=array();

            $price=new CProductPrice();

            foreach($shoppingArray as $key => $value)
            {
                $totalcounts = $shoppingArray[$key]['counts'];
                // echo($key.'--');
                // echo($item->getitemfuid($item->getitemid($key)).'--');
                foreach($shoppingArray as $keya => $valuea)
                {
                    if($key && $keya)
                    {
                        if($key != $keya)
                        {
                            if ($item->getitemfuid($item->getitemid($key)) && $item->getitemfuid($item->getitemid($keya)) && $item->getitemfuid($item->getitemid($key)) == $item->getitemfuid($item->getitemid($keya)))
                            {
                                $totalcounts=$totalcounts+$shoppingArray[$keya]['counts'];
                            }
                        }
                    }
                }

                $priceArray=$price->getAllPrice($key);

                // $itemArray_merge=$item->getItemsByItemid($key,$shoppingArray[$key]['pattern_name'],$shoppingArray[$key]['counts']);
                $itemArray_merge=$item->getItemsByItemid($key,$shoppingArray[$key]['pattern_name'], $totalcounts);

                $itemArray_merge[0]['min_counts']=$priceArray[0]['min_counts'];

                $itemArray_merge[0]['max_counts']=$priceArray[count($priceArray)-1]['max_counts'];

                $itemArray_merge[0]['buy_pattern_size']=$shoppingArray[$key]['pattern_size'];
                
                $itemArray_merge[0]['buy_BadgePatch']=$shoppingArray[$key]['BadgePatch'];

                if($shoppingArray[$key]['printAmounts'] && $shoppingArray[$key]['printAmounts']!=0)
                {
                $itemArray_merge[0]['buy_name_num']=$shoppingArray[$key]['name_num'];

                $itemArray_merge[0]['printMesg']=$shoppingArray[$key]['printMesg'];

                $itemArray_merge[0]['printAmounts']=$shoppingArray[$key]['printAmounts'];
                }
                array_unshift($itemArray,$itemArray_merge[0]);

                $itemidAmountsArray[$key.'-'.$shoppingArray[$key]['pattern_name']]=$shoppingArray[$key]['counts'];
            }

            foreach ($itemArray as &$value2)
            {

                $value2['amounts']=$itemidAmountsArray[$value2['itemid'].'-'.$value2['pattern_name']];

            }

            foreach ($itemArray as $value3)
            {

                if($value3['buy_name_num']=='')
                {

                    $totalPrice+=$value3['amounts']*$value3['net_price'];

                }else
                {

                    $totalPrice+=$value3['amounts']*$value3['net_price']+$value3['printAmounts']*2;

                }

                if($value3['buy_BadgePatch']=='Yes')
                {
                    $totalPrice+=$value3['amounts']*2;  //臂章加2美元
                }
            }
            $order=new COrder();
            $orderArray=$order->getAllOrderCounts($_COOKIE[COOKIE_PRE.'uid'],"","");

            if($orderArray)
            {
            }else
            {
                $totalPrice=$totalPrice-SENT;
            }
        }

        $view->assign('itemArray',$itemArray);

        $view->assign('totalPrice',$totalPrice);

        return array('itemArray'=>$itemArray,'totalPrice'=>$totalPrice);
    }

}
